/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class GttThemeActivity extends Activity implements OnClickListener, OnSeekBarChangeListener, android.content.DialogInterface.OnClickListener {
	ListView SampleList;
	ViewGroup ViewLayout,ColorLayout;
	
	Button CloseThemeBtn;
	TweetView SampleTweet;
	RadioButton UserRadio,ContentRadio,BackgroundRadio;
	SeekBar RSeek,GSeek,BSeek;
	TextView RText,GText,BText;
	Button DefBtn,SaveBtn,CancelBtn; 
	
	int [] [] ColorCache;
	SharedPreferences pref;
	int t_type;
	int t_view;
	
	private void loadPreferences() {
		pref = this.getSharedPreferences("uidata", MODE_PRIVATE);
		ColorCache = TweetView.getColorCache(this);
	}
	
	private void bindView() {
		ViewLayout = (ViewGroup)this.findViewById(R.id.theme_view_layout);
		ColorLayout = (ViewGroup)this.findViewById(R.id.theme_color_layout);
		
		SampleList = (ListView)this.findViewById(R.id.sample_lv);
		SampleList.setAdapter(new SampleListAdapter(this));
		CloseThemeBtn = (Button)this.findViewById(R.id.close_theme_btn);
		CloseThemeBtn.setOnClickListener(this);
		
		SampleTweet = (TweetView)this.findViewById(R.id.tweet_sample);
		UserRadio = (RadioButton)this.findViewById(R.id.user_radio);
		ContentRadio = (RadioButton)this.findViewById(R.id.content_radio);
		BackgroundRadio = (RadioButton)this.findViewById(R.id.background_radio);
		
		UserRadio.setOnClickListener(this);
		ContentRadio.setOnClickListener(this);
		BackgroundRadio.setOnClickListener(this);
		
		RSeek = (SeekBar)this.findViewById(R.id.r_seek);
		GSeek = (SeekBar)this.findViewById(R.id.g_seek);
		BSeek = (SeekBar)this.findViewById(R.id.b_seek);
		RSeek.setOnSeekBarChangeListener(this);
		GSeek.setOnSeekBarChangeListener(this);
		BSeek.setOnSeekBarChangeListener(this);
		
		RText = (TextView)this.findViewById(R.id.r_text);
		GText = (TextView)this.findViewById(R.id.g_text);
		BText = (TextView)this.findViewById(R.id.b_text);
			
		DefBtn = (Button)this.findViewById(R.id.def_btn);
		SaveBtn = (Button)this.findViewById(R.id.save_btn);
		CancelBtn = (Button)this.findViewById(R.id.cancel_btn);
		
		DefBtn.setOnClickListener(this);
		SaveBtn.setOnClickListener(this);
		CancelBtn.setOnClickListener(this);
		
	}

	public void showViewLayout() {
		ViewLayout.setVisibility(View.VISIBLE);
		ColorLayout.setVisibility(View.INVISIBLE);
	}

	public void showColorLayout() {
		prepareColorLayout();
		ViewLayout.setVisibility(View.INVISIBLE);
		ColorLayout.setVisibility(View.VISIBLE);
	}
	
	private void prepareColorLayout() {
		color[0] = ColorCache[t_type][0];
		color[1] = ColorCache[t_type][1];
		color[2] = ColorCache[t_type][2];
		UserRadio.setChecked(true);
		this.onClick(UserRadio);
	}
	
	private void setRGB(int RGBValue) {
		int b = RGBValue & 0xFF;
		int g = ( RGBValue >> 8 ) & 0xFF;
		int r = ( RGBValue >> 16 ) & 0xFF;
		RSeek.setProgress(r);
		GSeek.setProgress(g);
		BSeek.setProgress(b);
		RText.setText(String.format("%02X", r));
		GText.setText(String.format("%02X", g));
		BText.setText(String.format("%02X", b));
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.theme);
		loadPreferences();
		bindView();
		showViewLayout();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.user_radio:
			t_view = 0;
			setRGB(color[t_view]);
			break;
		case R.id.content_radio:
			t_view = 1;
			setRGB(color[t_view]);
			break;
		case R.id.background_radio:
			t_view = 2;
			setRGB(color[t_view]);
			break;
		case R.id.def_btn:
			new AlertDialog.Builder(this)
				.setTitle("use default colors?")
				.setPositiveButton("yes", this)
				.setNegativeButton("no", null)
				.show();
			break;
		case R.id.save_btn:
			ColorCache[t_type][0]=color[0];
			ColorCache[t_type][1]=color[1];
			ColorCache[t_type][2]=color[2];
			pref.edit()
				.putInt(String.format("color_%du", t_type),ColorCache[t_type][0])
				.putInt(String.format("color_%dc", t_type),ColorCache[t_type][1])
				.putInt(String.format("color_%db", t_type),ColorCache[t_type][2])
				.commit();
			((SampleListAdapter)SampleList.getAdapter()).notifyDataSetChanged();
			this.showViewLayout();
			break;
		case R.id.cancel_btn:
			this.showViewLayout();
			break;
		case R.id.close_theme_btn:
			finish();
			break;
		}
	}

	int [] color = new int [3];
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		TextView tv;
		switch (seekBar.getId()) {
		case R.id.r_seek:
			color[t_view] &= 0xff00FFFF;
			color[t_view] |= progress << 16;
			tv= RText;
			break;
		case R.id.g_seek:
			color[t_view] &= 0xffFF00FF;
			color[t_view] |= progress << 8;
			tv= GText;
			break;
		case R.id.b_seek:
			color[t_view] &= 0xffFFFF00;
			color[t_view] |= progress;
			tv= BText;
			break;
		default:
			return;
		}
		tv.setText(String.format("%02X", progress));
		switch (t_view) {
			case 0:
				SampleTweet.UserText.setTextColor(color[0]);
				break;
			case 1:
				SampleTweet.MsgText.setTextColor(color[1]);
				break;
			case 2:
				SampleTweet.setBackgroundColor(color[2]);
				break;
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		color[0]=0xff606060;
		color[1]=0xff000000;
		switch (t_type) {
		case 0:
			color[2] = 0xffFFDBBF;
			break;
		case 1:
			color[2] = 0xffD6BFFF;
			break;
		case 2:
			color[2] = 0xffBFF5FF;
			break;
		case 3:
			color[2] = 0xffFFF4BF;
			break;
		}
		onClick(SaveBtn);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0
				&& ColorLayout.getVisibility() == View.VISIBLE) {
			this.showViewLayout();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	
}
